#include <opencv2/highgui.hpp>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "opencv_helper.hpp"

class Planet
{
public:
    cv::Point center;
    int radius;
    cv::Scalar color;
    int thickness;

public:
    Planet(const cv::Point _center, int _radius, cv::Scalar _color, int _thickness=-1):
        center(_center), radius(_radius), color(_color), thickness(_thickness)
    {
    }
};

void update(cv::Mat& image, Planet& sun, Planet& earth, Planet& moon)
{
    image = cv::Scalar(0, 0, 0);
    static int earth_angle = 0;
    earth_angle = (earth_angle + 1) % 360;

    static int moon_angle = 0;
    moon_angle = (moon_angle + 6) % 360;

    //cv::circle(image, moon.center, moon.radius, moon.color, moon.thickness);
    //const float rho = hypot(sun.center.x - earth.center.x, sun.center.y - earth.center.y);
    //const float rho = 200;
    const int a = 200;
    const int b = 100;
    float theta = och::degree_to_radian(static_cast<float>(earth_angle));
    int x = a * cos(theta);
    int y = b * sin(theta);

    earth.center.x = sun.center.x + x;
    earth.center.y = sun.center.x + y;

    const int c = 50;
    const int d = 25;
    float phi = och::degree_to_radian(static_cast<float>(moon_angle));
    x = c * sin(moon_angle);
    y = d * cos(moon_angle);
    moon.center.x = earth.center.x + x;
    moon.center.y = earth.center.y + y;

    cv::circle(image, sun.center, sun.radius, sun.color, sun.thickness);
    cv::circle(image, earth.center, earth.radius, earth.color, earth.thickness);
    cv::circle(image, moon.center, moon.radius, moon.color, moon.thickness);
}

int main()
{
    const int w = 600;
    const int h = 600;
    cv::Size size(w, h);
    cv::Mat image(size, CV_8UC3);

    cv::Scalar black(0, 0, 0);
    cv::Scalar yellow(0, 255, 255);
    cv::Scalar green(0, 255, 0);

    image = black;

    Planet sun(cv::Point(w / 2, h / 2), 40, yellow);
    Planet earth(cv::Point(w / 2, h / 2), 20, green);
    Planet moon(cv::Point(w / 2, h / 2), 10, yellow);

    while (true)
    {
        update(image, sun, earth, moon);

        cv::imshow("image", image);
        cv::waitKey(20);
    }
    
    return 0;
}

